package com.zy.controller;

import com.zy.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.*;

/**
 * @author LittleDesire
 * @description
 * @date 2022-03-31 周四 9:27
 * @since 17
 */
@RestController
public class DemoController {


    @GetMapping("t4")
    public Object t4(@Validated User user, BindingResult br) {
        if (br.hasErrors()) {
//            return br.getAllErrors();
            Set<String> set = new HashSet<>();
            for (ObjectError allError : br.getAllErrors()) {
                set.add(allError.getDefaultMessage());
            }
            return set;
        }
        return user;
    }

    @GetMapping("t3")
    public Object t3() {
        Random random = new Random();
        int i = random.nextInt(1, 4);
        if (i == 1) {
            return LocalDate.now();
        } else if (i == 2) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", "200");
            return map;
        } else {
            Set<String> set = new HashSet<>();
            set.add("java");
            set.add("python");
            return set;
        }
    }


    @GetMapping("/t2")
    public Date t2(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date date) {
        return date;
    }
    @GetMapping("/t1")
    public Map<String, Object> t1() {

        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("msg", "成功");
        map.put("birth", LocalDate.now());
        map.put("date", new Date());
        map.put("addrs", new String[]{"北京", "上海", "南京"});
        return map;
    }
}
